<?php

namespace app\admin\controller;

class Menu extends Base
{
    protected function _infoModule()
    {
        return array(
            'info' => array(
                'name' => '管理菜单',
                'description' => '站点运行信息',
            ),
            'menu' => array(
                array(
                    'name' => '列表',
                    'url' => url('menu/index'),
                    'icon' => 'list',
                ),
            ),
            '_info' => array(
                array(
                    'name' => '添加',
                    'url' => url('menu/info'),
                ),
            ),
        );
    }

    public function index()
    {
        $list = model('AdminMenu')->loadList();
        $this->assign('list', $list);

        $this->fetch('index');
    }

    //后台菜单
    public function info()
    {
        $request = $this->request;
        $modelAdminMenu = model('AdminMenu');
        $method = $request->method();
        if ($method == 'POST') {
            $id = $request->post('id');
            if ($id) {
                $status = $modelAdminMenu->editAll($request);
            } else {
                $status = $modelAdminMenu->addall($request);
            }
            if ($status !== false) {
                jsonApi(1, '操作成功', ['url' => url('menu/index')]);
            } else {
                jsonApi(0, '操作失败');
            }
        } else {
            $id = $request->get('id');
            $info = $modelAdminMenu->getInfo($id);
            $this->assign('info', $info);

            if (empty($info['act'])) {
                $start_act = 1;
            } else {
                $start_act = count($info['act']) + 1;
            }
            $this->assign('start_act', $start_act);

            $this->assign('menuList', $modelAdminMenu->loadList());

            $this->fetch('info');
        }
    }

    //菜单删除
    public function del()
    {
        $request = $this->request;
        $id = $request->post('id');
        if (empty($id)) {
            jsonApi(0, '参数不能为空');
        }

        $modelAdminMenu = model('AdminMenu');
        //判断子栏目
        if ($modelAdminMenu->loadList(array(), $id)) {
            jsonApi(0, '请先删除子菜单！');
        }
        //删除栏目操作
        if ($modelAdminMenu->del($id)) {
            jsonApi(1, '栏目删除成功！');
        } else {
            jsonApi(0, '栏目删除失败！');
        }
    }

}
